Imports TreeCreeper01
Imports TreeCreeper01Interfaces

'This is a GUI class which performs a batch of random simulations for a non-spatial study.

Public Class cRandomBatchRun

    Private pDiversitiesBaseOne() As Double
    Private pCount As Integer
    Private pMean As Double
    Private pStandardDeviation As Double

    Public ReadOnly Property Count() As Integer
        Get
            Return pCount
        End Get
    End Property

    Public ReadOnly Property Mean() As Double
        Get
            Return pMean
        End Get
    End Property

    Public ReadOnly Property StandardDeviation() As Double
        Get
            Return pStandardDeviation
        End Get
    End Property

    Public ReadOnly Property DiversityResultByIndexBaseOne(ByVal Index As Integer) As Double
        Get
            DiversityResultByIndexBaseOne = pDiversitiesBaseOne(Index)
        End Get
    End Property

    Friend Sub DoABatchOfRuns(ByVal Study As IStudyNonSpatial, ByVal HowManyRuns As Integer, _
        ByVal HowManySpecies As Integer, ByVal DoUseAbundances As Boolean)

        Dim BatchIndex As Integer
        Dim BaseSpeciesList As ISpeciesAbundanceList

        BaseSpeciesList = Study.CreateBaseSpeciesAbundanceList(Study.Abundances)

        pCount = HowManyRuns
        ReDim pDiversitiesBaseOne(pCount)

        For BatchIndex = 1 To pCount
            pDiversitiesBaseOne(BatchIndex) = DoOneRun(BaseSpeciesList, HowManySpecies, DoUseAbundances)
        Next
        pMean = CalculateMean()
        pStandardDeviation = CalculateStandardDeviation(Mean)

    End Sub

    Private Function DoOneRun(ByVal BaseSpeciesList As ISpeciesAbundanceList, ByVal HowManySpecies As Integer, ByVal UseAbundances As Boolean) As Double

        Dim ListFilter As IListFilter
        Dim FilteredSpeciesList As ISpeciesAbundanceList
        Dim Diversity As Double

        'Calculate basic diversity for random list species drawn from the full list

        ListFilter = cListFilterCustomisation.CreateNonSpatialListFilterForRandomSpecies(BaseSpeciesList, HowManySpecies, UseAbundances)

        FilteredSpeciesList = BaseSpeciesList.NewSpeciesListByListFilter(ListFilter)

        Diversity = FilteredSpeciesList.SpeciesListAnalysis.CalculateGenericNonSpatialTaxonomicDiversity

        Return Diversity

    End Function

    Private Function CalculateMean() As Double

        Dim Count As Integer
        Dim Index As Integer
        Dim Accumulator As Double

        Count = UBound(pDiversitiesBaseOne, 1)

        For Index = 1 To Count
            Accumulator += pDiversitiesBaseOne(Index)
        Next

        Return (Accumulator / Count)

    End Function

    Private Function CalculateStandardDeviation(ByVal Mean As Double) As Double

        Dim Count As Integer
        Dim Index As Integer
        Dim ThisOne As Double
        Dim Accumulator As Double

        Count = UBound(pDiversitiesBaseOne, 1)

        For Index = 1 To Count
            ThisOne = pDiversitiesBaseOne(Index)
            ThisOne = ThisOne - Mean
            ThisOne = ThisOne ^ 2
            Accumulator += ThisOne
        Next

        ThisOne = Accumulator / (Count - 1)

        Return (ThisOne ^ 0.5)

    End Function

End Class
